package com.example.mypra1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 注册
 */
public class RegisterActivity extends AppCompatActivity {
    private Spinner spinner1,spinner2;
    private String [] shengxiao =new String[]{"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
    private String [] xinzuo = new String[]{"白羊座","金牛座","双子座","巨蟹座","狮子座",
            "处女座","天枰座","天蝎座","射手座","魔蝎座","水瓶座","双鱼座"};
    private EditText username_edit,phone_edit,password_edit;
    private RadioGroup sex;
    private Button button;
    private TextView textView;

   private String username ;
   private String phone ;
   private String password;
   private int sex_content = 0;
   private String zodiac;//生肖
   private String constellation;//星座

    private Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            int code = (int) msg.obj;
            if(code==0){
                Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }else{
                Toast.makeText(RegisterActivity.this,"注册失败!",Toast.LENGTH_LONG).show();
            }


        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username_edit = findViewById(R.id.reg_name);
        phone_edit = findViewById(R.id.reg_phone);
        password_edit = findViewById(R.id.reg_password);
        spinner1 = findViewById(R.id.spinner_shengxiao);
        spinner2 = findViewById(R.id.spinner_xinzuo);
        sex = findViewById(R.id.sex);
        textView = findViewById(R.id.go_login);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//跳转至登录界面
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //选择生肖
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
                zodiac = shengxiao[pos];
                Log.d("shengxiao", shengxiao[pos]);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });
        //选择星座
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                constellation = xinzuo[i];
                Log.d("xinzuo",xinzuo[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        button = findViewById(R.id.register);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = username_edit.getText().toString();
                phone = phone_edit.getText().toString();
                password = password_edit.getText().toString();
                for(int i=0;i<sex.getChildCount();i++){
                    RadioButton rd = (RadioButton) sex.getChildAt(i);
                    if(rd.isChecked()){
                        sex_content = i;
                        Log.d("sex",rd.getText().toString());
                        break;
                    }
                }
                getData(username,phone,password,sex_content,zodiac,constellation);
            }
        });
    }
    //发送post请求，增加一个用户
    private void getData(String username,String phone,String password,int sex,String zodiac,String constellation){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    FormBody formBody = new FormBody.Builder()
                            .add("username",username).add("birth","2022-09-01")
                            .add("phone",phone).add("password",password).add("sex",String.valueOf(sex))
                            .add("zodiac",zodiac).add("constellation",constellation).build();
                    Request request = new Request.Builder()
                            .url("http://8.142.19.67:3000/register")
                            .post(formBody)
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                            if (response.isSuccessful()) {
                                String res = response.body().string();
                                try {
                                    JSONObject jsonObject = new JSONObject(res);
                                     int code = jsonObject.getInt("code");
                                        Message message = new Message();
                                        message.obj = code;
                                        handler.sendMessage(message);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
